var refreshShoppingCartFlag = true;
$(function() {
    refreshShoppingCartCount();
    refreshNickName();
    $(".e-header-shopping-bag").on("shoppingBag.show-before",
        function() {
            refreshShoppingCartCount();
            if (getShoppingCartCount() > 0) {
                if ($(".sub-menu-full .content").hasClass("e-min-cart-empty-tinyscrollbar")) {
                    refreshShoppingCartFlag = true
                }
            } else {
                if ($(".sub-menu-empty .tinyscrollbar").hasClass("e-min-cart-tinyscrollbar")) {
                    refreshShoppingCartFlag = true
                }
            }
            refreshMiniShoppingCart(true)
        });
    $(".search-wrap .e-header-search").on("tap",
        function() {
            var a = $(".searchKeywords").val();
            if (isNullOrEmpty(a)) {
                a = $(".searchKeywords").attr("placeholder")
            }
            $("#keysearch").find("#keywords").val(a);
            $("#keysearch").submit()
        });
    $(".search-wrap .form-input input").keydown(function(b) {
        if (b.keyCode == "13") {
            var a = $(".searchKeywords").val();
            if (isNullOrEmpty(a)) {
                a = $(".searchKeywords").attr("placeholder")
            }
            $("#keysearch").find("#keywords").val(a);
            $("#keysearch").submit()
        }
    });
    $(".product-details-immediatelybuy").on("tap",
        function() {
            immediatelyBuy($(this).attr("data-code"))
        });
    $(".loginpullcatpchaCode").hide();
    $("#loginpullPassword").on("blur",
        function() {
            checkPullLoginpassword()
        });
    $("#loginpullName").on("blur",
        function() {
            $(".loginpullcatpchaCode").hide();
            checkPullLoginname()
        });
    $("#loginpullcatpchaCode").on("blur",
        function() {
            var a = $("#loginpullcatpchaCode").val();
            checkLoginPullCatpch(a)
        });
    $("#loginPullLogin").on("tap",
        function() {
            pullSubmit()
        });
    $(".loginpullpassword .form-input input").keydown(function(a) {
        if (a.keyCode == "13") {
            pullSubmit()
        }
    });
    $(".loginpullcatpchaCode .form-input input").keydown(function(a) {
        if (a.keyCode == "13") {
            pullSubmit()
        }
    });
    $(".remeberMePullPage").find(".e-checkbox").on("tap",
        function() {
            var a = $(this);
            if (a.hasClass("is-active")) {
                a.removeClass("is-active")
            } else {
                a.addClass("is-active")
            }
        });
    $(".form-input input", ".myaccount-login-wrapper").on("input propertychange",
        function() {
            var c = $(this),
                a = c.val(),
                b = c.parents(".form-input");
            if (a > 0) {
                b.addClass("is-success")
            } else {
                b.removeClass("is-success")
            }
        });
    if (isMobile()) {
        $(".icon-sign-in-with-weixin").parents("li:not(.e-header-login)").hide()
    }
    $(".e-header-login .login-text").on("mouseenter",
        function() {
            var d = loxia.syncXhrGet("/check/login", {});
            if (d.result) {
                $(".remeberMePullPage").find(".e-checkbox").addClass("is-active");
                $("#loginpullName").val(d.statusCode)
            } else {
                $(".remeberMePullPage").find(".e-checkbox").removeClass("is-active");
                $("#loginpullName").val("")
            }
            var c = $("#loginpullName").val();
            var a = {
                loginName: c
            };
            var b = loxia.syncXhrGet("/member/login/failCount.json", a);
            if (b) {
                $(".loginpullcatpchaCode").show()
            } else {
                $(".loginpullcatpchaCode").hide()
            }
        })
});
function closeShoppingBag() {
    Y.dialog.shoppingBagDialog.hide()
}
function closeShoppingBag() {
    Y.dialog.shoppingBagDialog.hide()
}
function loginOrRegister() {
    var d = loxia.syncXhrGet("/member/register/get/catpchCode", {});
    $(".registercatpchaCode").find(".code").html(d);
    var d = loxia.syncXhrGet("/check/login", {});
    if (d.result) {
        $(".remeberMe").find(".e-checkbox").addClass("is-active");
        $("#loginName").val(d.statusCode)
    } else {
        $(".remeberMe").find(".e-checkbox").removeClass("is-active");
        $("#loginName").val("")
    }
    var c = $("#loginName").val();
    var a = {
        loginName: c
    };
    var b = loxia.syncXhrGet("/member/login/failCount.json", a);
    if (b) {
        $(".logincatpchaCode").show()
    } else {
        $(".logincatpchaCode").hide()
    }
    Y.dialog.signUpDialog.show()
}
function loginOut() {
    var c = loxia.syncXhrGet("/member/login/logout.json", {});
    if (c.result) {
        var a = c.statusCode;
        var b = false;
        if (window.ActiveXObject || "ActiveXObject" in window) {
            if (a.indexOf("search") > -1 || a.indexOf("p=") > -1) {
                a = window.location.href
            }
        }
        if (a.indexOf("&amp;") > -1) {
            a = a.replace(/&amp;/g, "&");
            if (window.ActiveXObject || "ActiveXObject" in window) {
                if (a.indexOf("search") > -1 || a.indexOf("p=") > -1) {
                    a = window.location.href
                }
            }
        } else {
            if (a.indexOf("index") > -1) {
                a = "/"
            } else {
                if (a.indexOf("myaccount") > -1) {
                    a = "/member/login"
                }
            }
        }
        window.location.href = a
    }
}
var isNullOrEmpty = function(a) {
    if (a != undefined && a != null && a != "" && a != "undefined" && $.trim(a) != "") {
        return false
    } else {
        return true
    }
};
var addShoppingCart = function(a, e, d, g, f, c, b) {
    $.ajax({
        type: "POST",
        data: {
            skuId: a,
            count: e,
            engravedCode: c,
            engravedName: b
        },
        url: base + "/shoppingcart/add.json",
        dataType: "json",
        success: function(k) {
            if (k.result) {
                if (document.body.clientWidth > 1024) {
                    var j = {
                        skuId: a,
                        count: e
                    };
                    var h = loxia.syncXhrGet("/item/addCartSuccessShow.json", j);
                    if (!isNullOrEmpty(h) && document.body.clientWidth > 1024) {
                        var l = Y.dialog.shoppingBagDialog;
                        $(".dialog-container", l.elem).html(h);
                        l.show()
                    }
                } else {
                    showToastAlert("已成功加入购物袋")
                }
                if (d) {
                    var i = Y.dialog.minPdpDialog;
                    i.hide()
                }
                refreshShoppingCartFlag = true;
                refreshMiniShoppingCart(false)
            } else {
                if (!isNullOrEmpty(k.resultMessage)) {
                    buyItemAndAddShoppingCartValidate(k.resultMessage.message)
                } else {
                    if (k.statusCode == "third.member.notconsummate") {
                        window.location.href = base + "/member/showBind"
                    } else {
                        showToastAlert("添加购物车失败!")
                    }
                }
            }
        }
    })
};
var batchAddShoppingCart = function(b) {
    var e = loxia.syncXhrPost(base + "/shoppingcart/addmoreItemCode", b);
    if (isNullOrEmpty(e)) {
        showToastAlert("加入购物车失败，请重新选购");
        return
    }
    if (e.result) {
        showToastAlert("已成功加入购物袋");
        refreshShoppingCartFlag = true;
        refreshMiniShoppingCart(false)
    } else {
        if (isNullOrEmpty(e.resultMessage)) {
            showToastAlert("加入购物车失败，请重新选购");
            return
        }
        if (isNullOrEmpty(e.resultMessage.message)) {
            showToastAlert("加入购物车失败，请重新选购");
            return
        }
        if (e.resultMessage.message == "EXISTS_NOT_ENGRAVED_SKU") {
            showToastAlert("同款同色刻字商品不能与非刻字产品同时下单!");
            return
        }
        if (e.resultMessage.message == "DUPLICATE_ENGRAVED_SKU") {
            showToastAlert("购物车中只能存在一个同款同色的刻字商品!");
            return
        }
        var f = e.resultMessage.message.split("=");
        if (isNullOrEmpty(f[1])) {
            showToastAlert("加入购物车失败，请重新选购");
            return
        }
        var c = f[1];
        var d = c.substring(0, c.length - 1);
        if (isNullOrEmpty(d)) {
            showToastAlert("加入购物车失败，请重新选购");
            return
        }
        var a = "";
        if (d == "SKU_NOT_EXIST" || d == "SKU_NOT_ENABLE" || d == "ITEM_STATUS_NOT_ENABLE") {
            a = "商品不存在，请重新选购"
        } else {
            if (d == "TOTAL_MAX_THAN_QUANTITY" || d == "MAIN_LINE_MAX_THAN_COUNT") {
                a = "您的购物车添加商品已达到上限，请删除部分商品再添加"
            } else {
                if (d == "MAX_THAN_INVENTORY") {
                    a = "您添加的商品库存不足，请重新选购"
                } else {
                    if (d == "ONE_LINE_MAX_THAN_COUNT" || d == "ONE_LINE_MAX_THAN_COUNT_AFTER_MERGED") {
                        a = "您的购物车添加商品已达到上限，请删除部分商品再添加"
                    }
                }
            }
        }
        showToastAlert(a);
        return
    }
};
function refreshNickName() {
    var f = decodeURIComponent($.spice.getCookie("ysl_name"));
    f = decrypt(f);
    if (f != undefined && f != null && f != "") {
        $(".e-header-login").addClass("is-logged");
        var b = new RegExp(/^1[3|4|5|6|7|8][0-9]{9}$/);
        var a = b.test(f);
        var d = document.URL;
        if (a) {
            if ($(".nickNameSub").length != 0) {
                var c = "欢迎：" + f.substring(0, 3) + "****" + f.substring(7, 11);
                $(".nickNameSub")[0].innerText = c;
                $(".login-icon").attr("href", "/myaccount/myaccount")
            }
        } else {
            if ($.spice.getCookie("ysl_name").indexOf("+") > 0) {
                f = f.replace(/\+/g, " ")
            }
            if ($(".nickNameSub").length != 0) {
                var e = "欢迎：" + f;
                $(".nickNameSub")[0].innerText = e;
                $(".login-icon").attr("href", "/myaccount/myaccount")
            }
        }
    } else {
        $(".e-header-login").removeClass("is-logged");
        $(".login-icon").attr("href", "/member/login");
        $.spice.delCookie("ysl_name")
    }
}
function decrypt(c) {
    var b = CryptoJS.enc.Utf8.parse("abcdefgabcdefg12");
    var a = CryptoJS.AES.decrypt(c, b, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });
    return CryptoJS.enc.Utf8.stringify(a).toString()
}
var refreshMiniShoppingCart = function(a) {
    if (!refreshShoppingCartFlag) {
        return
    } else {
        refreshShoppingCartFlag = false
    }
    $.ajax({
        type: "GET",
        url: base + "/header/cart",
        dataType: "html",
        success: function(b) {
            $(".sub-menu-empty").empty();
            $(".sub-menu-full").empty();
            $(".sub-menu-empty").append(b);
            $(".sub-menu-full").append(b);
            Y.shoppingBagSubMenu.isEmpty = false;
            if (!a) {
                if ($(".e-header-shopping-bag").hasClass("is-empty")) {
                    $(".e-header-shopping-bag").removeClass("is-empty");
                    $(".e-header-shopping-bag").addClass("is-full")
                }
                Y.addShoppingBag.fn(2000)
            }
            refreshShoppingCartCount()
        }
    })
};
var refreshShoppingCartCount = function() {
    var a = getShoppingCartCount();
    if (a > 0) {
        if ($(".e-header-shopping-bag").hasClass("is-empty")) {
            $(".e-header-shopping-bag").removeClass("is-empty");
            $(".e-header-shopping-bag").addClass("is-full")
        }
    } else {
        if ($(".e-header-shopping-bag").hasClass("is-full")) {
            $(".e-header-shopping-bag").removeClass("is-full");
            $(".e-header-shopping-bag").addClass("is-empty")
        }
    }
    $(".icon-header-shopping-bag").attr("data-value", a);
    $(".event-shoppingcart-count").text("购物袋-" + a + "件商品")
};
var delMiniShoppingCartLine = function(d) {
    var e = d.data("id");
    var a = d.data("skuid");
    var c = {
        shoppingcartLineId: e,
        skuid: a
    };
    var b = window.location.href;
    if (b.indexOf("engraved") >= 0) {
        c.engravedFlag = 1
    }
    $.ajax({
        type: "POST",
        url: base + "/shoppingcart/delete.json",
        data: c,
        dataType: "json",
        success: function(g) {
            if (g.result) {
                if (getShoppingCartCount() == 0) {
                    var f = localHandlerbars.getTemplateFromCache("empty-mini-shoppingcart-template")();
                    d.parents(".header-sub-menu").hide()
                } else {
                    d.parents(".event-shoppingcartline").remove()
                }
                refreshShoppingCartCount();
                $(".event-mini-totalamount").text("¥" + g.returnObject)
            } else {
                showToastAlert("删除购物车行发生错误!")
            }
        }
    })
};
var getShoppingCartCount = function() {
    var a = $.spice.getCookie("s_c_c_n");
    if (a == "" || a == null || a == undefined) {
        return 0
    }
    return a
};
function searchAll(b) {
    var a = $(".searchKeywords").val();
    if (isNullOrEmpty(a)) {
        a = $(".searchKeywords").attr("placeholder")
    }
    $("#keysearch").find("#keywords").val(a);
    $("#keysearch").submit()
}
function buyItemAndAddShoppingCartValidate(a) {
    if (a == "notLogin") {
        memberDialog.show()
    } else {
        if (a == "BEYOND_MAX_LENGTH") {
            showToastAlert("超出购物车行最大购买数量")
        } else {
            if (a == "SKU_NOT_EXIST") {
                showToastAlert("商品不存在")
            } else {
                if (a == "SKU_NOT_ENABLE") {
                    showToastAlert("商品不可用")
                } else {
                    if (a == "ITEM_STATUS_NOT_ENABLE") {
                        showToastAlert("商品未上架")
                    } else {
                        if (a == "ITEM_NOT_ACTIVE_TIME") {
                            showToastAlert("商品未上架")
                        } else {
                            if (a == "ITEM_IS_GIFT") {
                                showToastAlert("赠品不可购买")
                            } else {
                                if (a == "ONE_LINE_MAX_THAN_COUNT" || a == "ONE_LINE_MAX_THAN_COUNT_AFTER_MERGED") {
                                    showToastAlert("超过可购买最大数量")
                                } else {
                                    if (a == "MAX_THAN_INVENTORY") {
                                        showToastAlert("库存不足")
                                    } else {
                                        if (a == "SHOPPING_CART_LINE_COMMAND_NOT_FOUND") {
                                            showToastAlert("购物车中未找到商品")
                                        } else {
                                            if (a == "EXISTS_NOT_ENGRAVED_SKU") {
                                                showToastAlert("同款同色刻字商品不能与非刻字产品同时下单!")
                                            } else {
                                                if (a == "EXISTS_NOT_SAME_ENGRAVED_SKU") {
                                                    showToastAlert("购物车中只能存在一个刻字商品!")
                                                } else {
                                                    if (a == "DUPLICATE_ENGRAVED_SKU") {
                                                        showToastAlert("购物车中只能存在一个同款同色的刻字商品!")
                                                    } else {
                                                        if (a == "MAIN_LINE_MAX_THAN_COUNT") {
                                                            showToastAlert("您的购物车添加商品已达到上限，请结算或删除部分商品再添加!")
                                                        } else {
                                                            if (a == "NOT_EXISTS_ENGRAVED_PROPERTY") {
                                                                showToastAlert("加入购物车失败,该商品不是刻字商品!")
                                                            } else {
                                                                showToastAlert("系统异常请稍后再试")
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
function couponErrorStatusMsg(b) {
    if (b == "EMPTY_SHOPPINGCART") {
        showToastAlert("空购物车无法使用促销代码!")
    } else {
        if (b == "2" || b == "COUPON_NOT_EXISTS") {
            showToastAlert("促销代码不存在，请及时联系客服 !")
        } else {
            if (b == "3") {
                showToastAlert("促销代码已被兑换 !")
            } else {
                if (b == "4") {
                    showToastAlert("促销代码已过期!")
                } else {
                    if (b == "5") {
                        showToastAlert("促销代码重复!")
                    } else {
                        if (b == "6") {
                            showToastAlert("促销代码类型重复!")
                        } else {
                            if (b == "7") {
                                showToastAlert("促销代码不能同享!")
                            } else {
                                if (b == "8") {
                                    showToastAlert("同一个月只能使用一次小美盒优惠券!")
                                } else {
                                    if (b == "0" || b == "UNSATISFIED_ACTIVE_CONDITIONS") {
                                        showToastAlert("促销代码不满足活动要求!")
                                    } else {
                                        if (b == "9") {
                                            showToastAlert("促销代码还未启用，请确认代码有效时间!")
                                        } else {
                                            if (b == "EXCESS_GIFT_MAX_QUANTITY") {
                                                showToastAlert("超出赠品最大赠送数量!")
                                            } else {
                                                if (b == "USE_COUNPON_MONTH") {
                                                    showToastAlert("同一个月只能使用一次付邮试用赠送的优惠券!")
                                                } else {
                                                    if (isNullOrEmpty(b)) {
                                                        showToastAlert("系统异常请稍后再试!")
                                                    } else {
                                                        if (b.indexOf("/") > 0) {
                                                            var a = b.split("/");
                                                            showToastAlert(a[1])
                                                        } else {
                                                            showToastAlert("系统异常请稍后再试!")
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return
}
function showToastAlert(a) {
    $.spice.dialog({
        dialogClass: "lancome-dialog lancome-auto-close-dialog",
        showClose: false,
        content: a,
        autoClose: 2000,
        initEvent: function(c) {
            var b = c.data.dialog;
            $(".dialog-close", b.elem).remove()
        }
    }).show()
}
function errorDialogShow(a) {
    var b = $.spice.dialog({
        dialogClass: "lancome-dialog lancome-toast-dialog",
        content: a,
        autoClose: 2000,
        showMask: false,
        initEvent: function(d) {
            var c = d.data.dialog;
            $(".dialog-close", c.elem).remove()
        }
    });
    b.show()
}
var immediatelyBuy = function(a) {
    $.ajax({
        type: "GET",
        url: base + "/quickview/" + a,
        data: {
            itemCode: a
        },
        dataType: "html",
        cache: false,
        success: function(b) {
            if (!isNullOrEmpty(b)) {
                var c = Y.dialog.minPdpDialog;
                $(".dialog-container", c.dialog.elem).html(b);
                c.eventInit();
                c.show()
            } else {
                showToastAlert("立即购买商品发生错误!")
            }
        }
    })
};
function pullSubmit() {
    if (checkPullLoginform()) {
        var a = $.trim($("#loginpullPassword").val());
        var e;
        if ($(".remeberMePullPage").find(".form-checkbox").hasClass("is-active")) {
            e = true
        } else {
            e = false
        }
        var c = {
            loginName: $("#loginpullName").val(),
            password: a,
            isRemberMeLoginName: e,
            logincatpchaCode: $("#loginpullcatpchaCode").val(),
            instanceId: $(".loginpullcatpchaCode").find(".code").find("input[type='hidden']").val(),
            userInput: $.trim($("#loginpullcatpchaCode").val()),
            captchaId: "captcha_pc_pullLogin",
        };
        var g = loxia.syncXhrPost("/member/login/login.json", c);
        if (g.result) {
            var b = g.statusCode.replace(/&amp;/g, "&");
            if (window.ActiveXObject || "ActiveXObject" in window) {
                if (b.indexOf("search") > -1 || b.indexOf("p=") > -1) {
                    b = window.location.href
                }
            }
            window.location.href = b
        } else {
            if (g.resultMessage.message == "login.captcha.error") {
                $(".loginpullcatpchaCode").find(".msg-block").find("span").html("请输入正确的验证码");
                $(".loginpullcatpchaCode").find(".msg-block").show();
                $(".loginpullcatpchaCode").find(".icon-input-success").hide();
                $(".loginpullcatpchaCode").find(".form-input").addClass("is-error");
                changeLoginPullCaptchaCode()
            } else {
                if (g.resultMessage.message == "login.failure") {
                    changeLoginPullCaptchaCode();
                    $(".remeberMePullPage").find(".login-error").removeClass("none")
                } else {
                    if (g.resultMessage.message == "old.member.repassword") {
                        changeLoginPullCaptchaCode();
                        window.location.href = "/member/forgetpassword/page?isNewMem=false"
                    } else {
                        changeLoginPullCaptchaCode();
                        $(".loginpullpassword").find(".msg-block").find("span").html("系统错误,请稍后再试");
                        $(".loginpullpassword").find(".msg-block").show();
                        $(".loginpullpassword").find(".form-input").addClass("is-error")
                    }
                }
            }
        }
        var f = $("#loginpullName").val();
        var c = {
            loginName: f
        };
        var d = loxia.syncXhrGet("/member/login/failCount.json", c);
        if (d) {
            $(".loginpullcatpchaCode").show()
        }
    }
}
function checkPullLoginname() {
    var g = $("#loginpullName").val();
    var b = false;
    var c = new RegExp(/^1[3|4|5|6|7|8][0-9]{9}$/);
    var h = new RegExp(/^[A-Za-z0-9_.-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/);
    var a = c.test(g);
    var f = h.test(g);
    var d = {
        loginName: g
    };
    var e = loxia.syncXhrGet("/member/login/failCount.json", d);
    if (g == "") {
        $(".loginpullname").find(".msg-block").show();
        $(".loginpullname").find(".form-input").addClass("is-error");
        b = false
    } else {
        if (!a && !f) {
            $(".loginpullname").find(".msg-block").show();
            $(".loginpullname").find(".form-input").addClass("is-error");
            b = false
        } else {
            $(".loginpullname").find(".msg-block").hide();
            $(".loginpullname").find(".form-input").removeClass("is-error");
            b = true
        }
    }
    if (e) {
        $(".loginpullcatpchaCode").show();
        $(".loginpullcatpchaCode").find(".btn-group").show();
        b = false
    }
    return true
}
function checkPullLoginpassword() {
    var b = $("#loginpullPassword").val();
    var a = false;
    if (b == "") {
        $(".loginpullpassword").find(".msg-block").show();
        $(".loginpullpassword").find(".form-input").addClass("is-error");
        a = false
    } else {
        $(".remeberMePullPage").find(".login-error").addClass("none");
        $(".loginpullpassword").find(".msg-block").hide();
        $(".loginpullpassword").find(".form-input").removeClass("is-error");
        a = true
    }
    return a
}
function checkPullLoginform() {
    var a = false;
    if (checkPullLoginname() && checkPullLoginpassword()) {
        a = true
    } else {
        a = false
    }
    return a
}
function searchkey(b) {
    var a = $(b).attr("data-value");
    $("#keysearch").find("#keywords").val(a);
    $("#keysearch").submit()
}
function checkLoginPullCatpch(c) {
    var a = false;
    if (c == "") {
        $(".loginpullcatpchaCode").find(".msg-block").find("span").html("请输入4位随机验证码");
        $(".loginpullcatpchaCode").find(".msg-block").show();
        $(".loginpullcatpchaCode").find(".form-input").addClass("is-error");
        a = false
    } else {
        var d = $("#LBD_VCID_captcha_pc_pullLogin").val();
        var b = {
            randomCode: c,
            userInput: $("#loginpullcatpchaCode").val(),
            instanceId: d,
            captchaId: "captcha_pc_pullLogin",
        };
        var e = loxia.syncXhrGet("/member/register/check/loginCode.json", b);
        if (e.result) {
            $(".loginpullcatpchaCode").find(".msg-block").hide();
            $(".loginpullcatpchaCode").find(".form-input").removeClass("is-error");
            a = true
        } else {
            $(".loginpullcatpchaCode").find(".msg-block").find("span").html("请输入正确的验证码");
            $(".loginpullcatpchaCode").find(".msg-block").show();
            $(".loginpullcatpchaCode").find(".form-input").addClass("is-error");
            a = false;
            changeLoginPullCaptchaCode()
        }
    }
    return a
}
function changeLoginPullCaptchaCode() {
    var a = loxia.syncXhrGet("/member/register/get/loginCatpchCode", {});
    $(".loginpullcatpchaCode").find(".code").html(a)
};